/*
 * Copyright 2012 ITC Tsinghua University. All Rights Reserved.
 */
package cn.edu.tsinghua.cic.ccss.bo.impl;

import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;
import org.forten.utils.system.PropertiesFileReader;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * 
 * @since 2012-6-8
 */
public class BoHelper {
	private static final String URL = PropertiesFileReader.getValue(
			"forten-system", "security.synchronized.url");
	private static final String APP_KEY = PropertiesFileReader.getValue(
			"forten-system", "security.synchronized.appkey");
	private static final String ROLE_ID = PropertiesFileReader.getValue(
			"forten-system", "security.synchronized.roleid");

	public static String synRoleToAuth(String cardNum)
			throws MalformedURLException, Exception {
		Client client = new Client(new URL(URL));
		Object[] results = client.invoke("createUserRole", new Object[] {
				APP_KEY, cardNum, ROLE_ID });
		String msg = "";
		switch ((Integer) results[0]) {
		case 0:
			msg = "0: Success";
			break;
		case 101:
			msg = "101: App Key error";
			throw new Exception(msg);
		case 102:
			msg = "102: Application no permission access current role";
			throw new Exception(msg);
		case 201:
			msg = "201: Role doesn't exist";
			throw new Exception(msg);
		case 202:
			msg = "202: Unable to change current role";
			throw new Exception(msg);
		case 203:
			msg = "203: no action";
			break;
		case 301:
			msg = "301: Fail to Auth";
			throw new Exception(msg);
		default:
			msg = "Unknowned Error";
			throw new Exception(msg);
		}
		return msg;
	}

	public static String delRoleToAuth(String cardNum)
			throws MalformedURLException, Exception {
		Client client = new Client(new URL(URL));
		Object[] results = client.invoke("deleteUserRole", new Object[] {
				APP_KEY, cardNum, ROLE_ID });
		String msg = "";
		switch ((Integer) results[0]) {
		case 0:
			msg = "0: Success";
			break;
		case 101:
			msg = "101: App Key error";
			throw new Exception(msg);
		case 102:
			msg = "102: Application no permission access current role";
			throw new Exception(msg);
		case 201:
			msg = "201: Role doesn't exist";
			throw new Exception(msg);
		case 202:
			msg = "202: Unable to change current role";
			throw new Exception(msg);
		case 301:
			msg = "301: Fail to Auth";
			throw new Exception(msg);
		default:
			msg = "Unknowned Error";
			throw new Exception(msg);
		}
		return msg;
	}
}
